/* =================================================
 * Package:			dk.iha.toksEditor
 * File:			User.java
 * Description:		This class is responsible to 
 * 					handle all the user information.
 * 					This is done by saving and loading
 * 					data from shared preferences

 * Responsible
 * =================================================
 * Name:			Vinnie Juul
 * Study number:	09767
 */

package dk.iha.toksEditor;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

public class User{

	public final String DOCTOR = "DOCTOR";
	public final String NURSE = "NURSE";
	private static final String myUserPrefs = "USER_PREFS";
	private final String USER_NAME = "userName";
	private final String USER_PASSWORD = "userPassword";
	private final String USER_IS_LOGGED_IN = "userIsLoggedIn";
	private final String USER_TYPE = "userType"; 
	private SharedPreferences mPrefs;
	private	SharedPreferences.Editor mEditor;
	private Context mContext;
	private DataBaseConnection mDBConnection;
	
	/**
	 * Setup the user class
	 * @param context - the context of the caller
	 */
	public User (Context context){
		mContext = context;
		mPrefs = mContext.getSharedPreferences(myUserPrefs, Activity.MODE_PRIVATE);
		mEditor = mPrefs.edit();
		mDBConnection = new DataBaseConnection(this);
	}
	
	/**
	 * Validate the input with the database to see if the user exist
	 * @param user - string; user name
	 * @param pass - string; password
	 * @return Boolean; true if user is exist
	 */
	public Boolean LogIn(String user, String pass)
	{
		String test = pass;
		if(test.length() == 0)
		{
			Toast.makeText(mContext, R.string.LogInNoPassword, Toast.LENGTH_SHORT).show();
			return false;
		}
		
		
		String prevUserName = getUserName();
		
		setUserName(user); // suv
		setPassword(pass); // sikkerhed
		
		if(mDBConnection.LogInOK()){
			String type = mDBConnection.getUserType();
			setType(type);
			logIn();
			return true;
		}
		
		else{
			setUserName(prevUserName);
			setPassword("");
			return false;
		}
	}

	/**
	 * @return, string; the name of the user
	 */
	public String getUserName() {
		return mPrefs.getString(USER_NAME, "").toString();
	}
	
	/**
	 * Set the user name
	 * @param mUserName - the name of the user
	 */
	public void setUserName(String mUserName) {
		mEditor.putString(USER_NAME, mUserName);
		mEditor.commit();
	}
	
	/**
	 * Get users password
	 * @return the password for the user
	 */
	public String getPassword() {
		return mPrefs.getString(USER_PASSWORD, "");
	}
	
	/**
	 * Set the password for the user
	 * @param mPassword - users password
	 */
	public void setPassword(String mPassword) {
		mEditor.putString(USER_PASSWORD, mPassword);
		mEditor.commit();
	}
	/**
	 * Get the type of the user
	 * Return NURSE or DOCTOR as string
	 */
	public String getType() {
		return mPrefs.getString(USER_TYPE, "NURSE");
	}

	/**
	 * Set the type of the user
	 * @param mType - user type, DOCTOR or NURSE
	 */
	public void setType(String mType) {
		if(mType.equals(DOCTOR))
			mEditor.putString(USER_TYPE, DOCTOR.toString());
		else
			mEditor.putString(USER_TYPE, NURSE.toString());
		mEditor.commit();
	}
	
	/**
	 * Find out if the user is logged in
	 * @return true if user is logged in, else false
	 */
	public boolean isLoggedIn(){
		return mPrefs.getBoolean(USER_IS_LOGGED_IN, false);
	}
	
	/**
	 * Save that the user is logged in
	 */
	private void logIn(){
		mEditor.putBoolean(USER_IS_LOGGED_IN, true);
		mEditor.commit();
	}
	
	/**
	 * Save that the user has logged out
	 */
	public void logOut(){
		mEditor.putBoolean(USER_IS_LOGGED_IN, false);
		mEditor.commit();
	}
}
